package com.ruoyi.logistic.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.logistic.domain.NetworkFeeSubject;
import com.ruoyi.logistic.domain.NetworkFeeSubjectMonth;
import com.ruoyi.logistic.domain.dto.NetworkFeeSubjectDto;
import com.ruoyi.logistic.domain.dto.NetworkFeeSubjectDtoTwo;
import com.ruoyi.logistic.domain.vo.NetworkFeeSubjectVo;
import com.ruoyi.logistic.mapper.NetworkFeeSubjectMapper;
import com.ruoyi.logistic.service.NetworkFeeSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class NetworkFeeSubjectServiceImpl implements NetworkFeeSubjectService {
    @Autowired
    private NetworkFeeSubjectMapper networkFeeSubjectMapper;

    @Override
    public List<NetworkFeeSubject> search(NetworkFeeSubjectVo vo) {
        List<NetworkFeeSubject> subjects = networkFeeSubjectMapper.search(vo);
        for (NetworkFeeSubject subject : subjects) {
            subject.setThisYearAmount(networkFeeSubjectMapper.collectYearAmount(
                    networkFeeSubjectMapper.findById(subject.getId()).getLabel().substring(0, 4),
                    networkFeeSubjectMapper.findById(subject.getId()).getName()));
            networkFeeSubjectMapper.update(subject);
        }
        return subjects;
    }

    @Override
    public boolean insert(NetworkFeeSubjectDto networkFeeSubject) {
        networkFeeSubject.setLevel(2);
        networkFeeSubject.setThisMonthAmount(new BigDecimal(0));
        networkFeeSubject.setThisYearAmount(new BigDecimal(0));
        networkFeeSubject.setRemark(null);
        return networkFeeSubjectMapper.insert(networkFeeSubject);
    }

    @Override
    public boolean updateList(List<NetworkFeeSubject> list) {
        for (NetworkFeeSubject subject : list) {
            networkFeeSubjectMapper.update(subject);
        }

        return true;
    }

    @Override
    public List<NetworkFeeSubject> selectOneLevel() {
        return networkFeeSubjectMapper.selectOneLevel();
    }

    @Override
    public List<NetworkFeeSubjectMonth> selectMonth() {
        List<NetworkFeeSubjectMonth> month = new ArrayList<>();
        NetworkFeeSubjectMonth month1 = new NetworkFeeSubjectMonth();
        month1.setYear("2024");
        month1.setMonth(networkFeeSubjectMapper.selectByMonth(month1.getYear()));
        month.add(month1);
        NetworkFeeSubjectMonth month2 = new NetworkFeeSubjectMonth();
        month2.setYear("2023");
        month2.setMonth(networkFeeSubjectMapper.selectByMonth(month2.getYear()));
        month.add(month2);
        return month;
    }

    @Override
    public String collectYearAmount(String month, String name) {
        return networkFeeSubjectMapper.collectYearAmount(month, name);
    }
}
